package com.police.controller;

import com.loopj.android.http.AsyncHttpClient;
import org.apache.http.HeaderElement;
import org.apache.http.HeaderElementIterator;
import org.apache.http.HttpResponse;
import org.apache.http.conn.ConnectionKeepAliveStrategy;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicHeaderElementIterator;
import org.apache.http.protocol.HTTP;
import org.apache.http.protocol.HttpContext;

/**
 * Created by Leo on 13-5-26.
 */
public abstract class Controller implements ICtrl {
    private static final int TIMEOUT = 40 * 1000;// 默认时长10s
    static AsyncHttpClient client = new AsyncHttpClient();

    public Controller(){
        client.setTimeout(TIMEOUT);
        ((DefaultHttpClient) client.getHttpClient())
                .setKeepAliveStrategy(new ConnectionKeepAliveStrategy() {

                    public long getKeepAliveDuration(HttpResponse response,
                                                     HttpContext context) {
                        // Honor 'keep-alive' header
                        HeaderElementIterator it = new BasicHeaderElementIterator(
                                response.headerIterator(HTTP.CONN_KEEP_ALIVE));
                        while (it.hasNext()) {
                            HeaderElement he = it.nextElement();
                            String param = he.getName();
                            String value = he.getValue();
                            if (value != null
                                    && param.equalsIgnoreCase("timeout")) {
                                try {
                                    return Long.parseLong(value) * 1000;
                                } catch (NumberFormatException ignore) {
                                }
                            }
                        }
                        return 15 * 1000;

                    }

                });

    }

    @Override
    public void post(IRequest request, IAsyncHandler handler) {

    }
}
